<?php

namespace iBrand\Shop\Core\Models;

use iBrand\Store\Backend\Model\Goods;
use Illuminate\Database\Eloquent\Model;

class O2oGoods extends Model
{

	protected $guarded = ['id'];

	public function __construct(array $attributes = [])
	{
		parent::__construct($attributes);

		$prefix = config('ibrand.app.database.prefix', 'ibrand_');

		$this->setTable($prefix . 'o2o_goods');
	}

	public function goods()
	{
		return $this->belongsTo(Goods::class, 'goods_id');
	}

	public function o2oProducts()
	{
		return $this->hasMany(O2oGoodsProducts::class, 'o2o_goods_id');
	}

	public function restoreStock($quantity)
	{
		$this->store_nums = $this->store_nums + $quantity;
		$this->save();
	}

	public function reduceStock($quantity)
	{
		$this->store_nums = $this->store_nums - $quantity;
		$this->save();
	}

	public function getStockQty()
	{
		return $this->store_nums;
	}

	public function getIsInSale($quantity)
	{
		if ($this->status == 1 && $this->store_nums >= $quantity) {
			return true;
		}

		return false;
	}

	public function increaseSales($quantity)
	{
		$this->sale = $this->sale + $quantity;
		$this->save();
	}

	public function restoreSales($quantity)
	{
		$this->sale = $this->sale - $quantity;
		$this->save();
	}
}